﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.BLL;
using CMN.Common;
using CMN.Entity;
using CMN.Models.Interfaces;
namespace CMN.Models
{
    /// <summary>
    /// 频道Service
    /// </summary>
    public class Normal_ChannelService: IDataService<Normal_ChannelEntity>
    {
        /// <summary>
        /// 新增频道
        /// </summary>
        /// <param name="Normal_ChannelEntity"></param>
        /// <returns>bool</returns>
        public string Insert(Normal_ChannelEntity normal_channelEntity)
        {
            Normal_Channel bll = new Normal_Channel();
            normal_channelEntity.DataTable_Action = DataTableAction.Insert;
            if (bll.InsertUpdateDelete(normal_channelEntity) > 0)
            {
                return "" ;
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 返回频道查询列表
        /// </summary>
        /// <param name="qp"></param>
        /// <param name="count">查询总个数</param>
        /// <returns>List</returns>
        public List<Normal_ChannelEntity> List(QueryParam qp,out int count)
        {
            Normal_Channel bll = new Normal_Channel();
            return bll.List(qp, out count);
        }

        /// <summary>
        /// 更新频道
        /// </summary>
        /// <param name="Normal_Channel"></param>
        /// <returns>bool</returns>
        public string Update(Normal_ChannelEntity normal_channelEntity)
        {
            Normal_Channel bll = new Normal_Channel();
            normal_channelEntity.DataTable_Action = DataTableAction.Update;
            if (bll.InsertUpdateDelete(normal_channelEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 删除频道
        /// </summary>
        /// <param name="Normal_Channel"></param>
        /// <returns>bool</returns>
        public string Delete(Normal_ChannelEntity normal_channelEntity)
        {
            Normal_Channel bll = new Normal_Channel();
            normal_channelEntity.DataTable_Action = DataTableAction.Delete;
            QueryParam qp = new QueryParam();
            normal_channelEntity=bll.Disp(normal_channelEntity.Id);
            qp.Where = string.Concat(" path like '", normal_channelEntity.Path, "%'");
            qp.PageSize = 2;
            int recourdNum=0;
            if (bll.List(qp, out recourdNum).Count > 1)
            {
                return "请先删除该栏目下的子栏目";
            }
            BLLHelper bllHelper=new BLLHelper();
            string strNum= bllHelper.ExecuteScalar("select count(1) from Module_"+normal_channelEntity.ChannelType +" where path='"+normal_channelEntity.Path+"'");
            if (Utils.StringToInt(strNum, 0) > 0)
            {
                return "请先删除该栏目下的文章";
            }
            normal_channelEntity.DataTable_Action = DataTableAction.Delete;
            if (bll.InsertUpdateDelete(normal_channelEntity) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }
        }

        /// <summary>
        /// 批量删频道
        /// </summary>
        /// <param name="idList">删除id列表  2_8_9_45</param>
        /// <returns>bool</returns>
        public string DeleteList(string idList)
        {
            Normal_Channel bll = new Normal_Channel();
            string[] list =Utils.SplitString(idList,"_");
            string strDel=Utils.ArrayToString(list,",",false);
            if (bll.DelList(strDel) > 0)
            {
                return "";
            }
            else
            {
                return "失败";
            }

        }

        /// <summary>
        /// 返回单个频道
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Normal_ChannelEntity</returns>
        public Normal_ChannelEntity Look(int id)
        {
            Normal_Channel bll = new Normal_Channel();
            return bll.Disp(id);
        }
    }
}
